-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cancel_status param to the cancel order action #1326
base: main
Are you sure you want to change the base?
Conversation
f6ca9c4
to
a45355a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I left a few comments for some minor changes.
We can get inspiration from #1325 for 1) the fewer/simpler changes to logics.py
, 2) the addition of comments into oas_schemas.py
and 3) ENUM_NAME_OVERRIDES
as this hack leads into a more complete api-latest
generated schema.
Thank you for implementing the test cases, this is very important.
We will merge this PR in favor of #1325 but both of them are technically good, so what if we split a bit the rewards? 70K for this PR and 40K for #1325.
docs/assets/schemas/api-latest.yaml
Outdated
@@ -469,6 +469,10 @@ paths: | |||
- `17` - Maker lost dispute | |||
- `18` - Taker lost dispute | |||
|
|||
The client can use `cancel_status` to cancel the order only |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc is unfortunately auto-generated when running test_trade_pipeline, so these strings here will likely be lost I believe. Maybe I am wrong. I think we should add these helpful comments on oas_schemas.py
just as #1325 and then the autogenerated api-latest.yaml
will always have this text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is auto generated in tests/test_api.py
at the start of the tests. Also without ENUM_NAME_OVERRIDES
and with this change api-latest.yaml
gets really polluted :)
api/logics.py
Outdated
@@ -1009,10 +1009,17 @@ def cancel_order(cls, order, user, state=None): | |||
if order.status in do_not_cancel: | |||
return False, {"bad_request": "You cannot cancel this order"} | |||
|
|||
# 1) When maker cancels before bond | |||
# 1) If the order status is not the one specified by the user, do not cancel the order. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best to leave this "do not cancel` comments without enumeration. That is, remove the "1)" , and leave all other cases with the number they had. This is consistent with the first cancel rejection case just above this one:
if order.status in do_not_cancel:
return False, {"bad_request": "You cannot cancel this order"}
The intention of enumerating was to have a clear map of the different cancellation conditions and bond slashing because they are all unique in some way.
api/logics.py
Outdated
# The order never shows up on the book and order | ||
# status becomes "cancelled" | ||
if order.status == Order.Status.WFB and order.maker == user: | ||
elif order.status == Order.Status.WFB and order.maker == user: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's leave this as it was for simplicity.
api/logics.py
Outdated
if cancel_status is not None and order.status != cancel_status: | ||
return False, { | ||
"bad_request": f"Order status {order.status} is not {cancel_status}. " + | ||
"The order may have been taken before it was cancelled" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message "The order may have been taken before it was cancelled"
only applies to a very specific case of order.status != cancel_status
. Maybe best remove it. Very often this bad_request will be returned because of a check between order status that is not public != taken
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes agree, also I think it is better to put this check before the if order.status in do_not_cancel
like in my pull request, because otherwise if the client has a wrong cancel_status
, it gets returned "You cannot cancel this trade", but this is referring to the correct order status. It may confuse a bit the client, it is better to first check if the cancel_status
is correct.
@Reckless-Satoshi I think that |
Too many #1325 :) But I understand, perfectly fine with me. |
5a3d63f
to
a9bf7f3
Compare
@Reckless-Satoshi Just applied the changes suggested, let me know if there's anything else I should modify 👍
That's totally fine with me, thanks @jerryfletcher21 for your contributions! |
robosats/middleware.py
Outdated
@@ -8,7 +8,6 @@ | |||
from django.utils.deprecation import MiddlewareMixin | |||
from django.http import JsonResponse | |||
from rest_framework.authtoken.models import Token | |||
from rest_framework.exceptions import AuthenticationFailed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this line to fix the lint check error
robosats/middleware.py:11:39: F401 [*] `rest_framework.exceptions.AuthenticationFailed` imported but unused
because the import is not used, but it failed with the same message in the run of the commit that removed it 🤔
It passed locally, may it need a re-run?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a linting error that made it to main
on a merge conflict resolution. Should now be fine if this branch is updated.
237682c
to
eb692bd
Compare
Yes, in fact, I removed it from |
Thank you, I actually don't know how to handle this situation 😅😂 Kudos is yours for thinking about sending the expected status. 😁 |
I'll need to test the frontend part before we merge. Will likely not be able until sunday. |
ad0af4c
to
6840242
Compare
Update: rebased the branch to pull the changes from
Alright, no problem 👍 |
6840242
to
27f1129
Compare
Update: rebased to the updated base branch and fixed a conflict in the |
I might be doing something wrong here. Currently 17 tests are failing for me ======================================================================
ERROR: test_withdraw_reward_after_unilateral_cancel (tests.test_trade_pipeline.TradeTest.test_withdraw_reward_after_unilateral_cancel)
Tests withdraw rewards as taker after maker cancels order unilaterally
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/robosats/tests/test_trade_pipeline.py", line 1226, in test_withdraw_reward_after_unilateral_cancel
trade.cancel_order(trade.maker_index)
File "/usr/local/lib/python3.12/unittest/mock.py", line 1390, in patched
return func(*newargs, **newkeywargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/robosats/tests/utils/trade.py", line 120, in cancel_order
self.response = self.client.post(path + params, body, **headers)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/rest_framework/test.py", line 296, in post
response = super().post(
^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/rest_framework/test.py", line 209, in post
data, content_type = self._encode_data(data, format, content_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/rest_framework/test.py", line 180, in _encode_data
ret = renderer.render(data)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/rest_framework/renderers.py", line 916, in render
return encode_multipart(self.BOUNDARY, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/django/test/client.py", line 300, in encode_multipart
raise TypeError(
TypeError: Cannot encode None for key 'cancel_status' as POST data. Did you mean to pass an empty string or omit the value |
27f1129
to
2bb4891
Compare
@Reckless-Satoshi That was caused by what we discussed here. I may have executed the tests with a cached environment so I didn't notice the error after I changed it (my bad). I've just updated that part of the code so it sets the |
9054b87
to
f9244e1
Compare
2bb4891
to
2d8d529
Compare
@Reckless-Satoshi Just rebased, solved some conflicts and ran the tests (results in the description) to validate that everything works as expected. Please let me know if there's anything else I should do from my side to get this merged. |
2d8d529
to
58f5ee1
Compare
72c9e74
to
1d24380
Compare
1d24380
to
ec0356f
Compare
What does this PR do?
Fixes #1311
This PR adds the
cancel_status
parameter to thePOST /api/order
endpoint for users to specify the status in which an order should be for the cancellation to take place.Checklist before merging
pip install pre-commit
, thenpre-commit install
. Pre-commit installs git hooks that automatically check the codebase. If pre-commit fails when you commit your changes, please fix the problems it points out.Tests
test_cancel_order_cancel_status
test_cancel_order_different_cancel_status